﻿using System.Xml.Linq;
using Xunit;
using Xunit.Should;

namespace Adjunct.Framework.Nuspec
{
	public class NuspecLibReferencesSpec
	{
		private const string ValidXml = "<references><reference file='Adjunct.Framework.dll' /><reference file='xunit.dll' /></references>";
		private readonly NuspecLibReference _reference1 = new NuspecLibReference("xunit.dll");
		private readonly NuspecLibReference _reference2 = new NuspecLibReference("Adjunct.Framework.dll");

		[Fact]
		public void EmptyToXElement()
		{
			NuspecLibReferences references = new NuspecLibReferences();
			((XElement)references).ShouldBeNull();
		}

		[Fact]
		public void FromEmptyXElement()
		{
			XElement element = XElement.Parse("<references />");
			NuspecLibReferences references = ((NuspecLibReferences)element);
			references.ShouldNotBeNull();
			references.ShouldBeEmpty();
		}

		[Fact]
		public void FromNullXElement()
		{
			XElement element = null;
			NuspecLibReferences references = ((NuspecLibReferences)element);
			references.ShouldNotBeNull();
			references.ShouldBeEmpty();
		}

		[Fact]
		public void FromValidXElement()
		{
			XElement element = XElement.Parse(ValidXml);
			((NuspecLibReferences)element).ShouldEqual(new NuspecLibReferences {
				_reference1,
				_reference2
			});
		}

		[Fact]
		public void NullToXElement()
		{
			NuspecLibReferences references = null;
			((XElement)references).ShouldBeNull();
		}

		[Fact]
		public void ValidToXElement()
		{
			NuspecLibReferences references = new NuspecLibReferences {
				_reference1,
				_reference2
			};
			((XElement)references).ShouldEqual(ValidXml);
			references = new NuspecLibReferences {
				_reference2,
				_reference1
			};
			((XElement)references).ShouldEqual(ValidXml);
		}
	}
}